﻿using SHKF.B2C.YouZanYun.Model.Inter.Category;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SHKF.B2C.Mannage.AdminUI.View.Yzy.Import
{
    /// <summary>
    /// 产品分类
    /// </summary>
    public partial class frmSearchProductCategory : Window
    {
        public string authority_id = "";

        private long _leaf_category_id = 0;

        public frmSearchProductCategory()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GL.HttpClientYzy.authority_id = this.authority_id;

            LoadItemCategory1();
        }

        private async void LoadItemCategory1()
        {
            CategoryRequest category1 = await GL.HttpClientYzy.GetCategoryChildren(0);
            if (category1.data != null)
            {
                foreach (CategoryData item in category1.data)
                {
                    CategoryRequest category2 = await GL.HttpClientYzy.GetCategoryChildren(item.id);
                    item.data = category2.data;
                }

                this.tvLevel1.ItemsSource = category1.data;
            }
            else
            {
                MessageBox.Show(category1.message);
            }
        }

        private void tvLevel1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tv = (TreeView)sender;
            CategoryData data = (CategoryData)tv.SelectedValue;
            LoadItemCategory(this.tvLevel2, data.id);
        }

        private void tvLevel2_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tv = (TreeView)sender;
            if (tv.SelectedValue != null)
            {
                CategoryData data = (CategoryData)tv.SelectedValue;
                LoadItemCategory(this.tvLevel3, data.id);
            }
        }

        private void tvLevel3_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tv = (TreeView)sender;
            if (tv.SelectedValue != null)
            {
                CategoryData data = (CategoryData)tv.SelectedValue;
                LoadItemCategory(this.tvLevel4, data.id);
            }
        }

        private async void LoadItemCategory(TreeView tv, long id)
        {
            CategoryRequest category = await GL.HttpClientYzy.GetCategoryChildren(id);
            if (category.data != null)
            {
                tv.ItemsSource = category.data;
            }
            else
            {
                MessageBox.Show(category.message);
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {

            if (this.tvLevel1.SelectedValue != null)
            {
                CategoryData data = (CategoryData)tvLevel1.SelectedValue;
                if (!data.has_children)
                {
                    _leaf_category_id = data.id;
                }
            }

            if (this.tvLevel2.SelectedValue != null)
            {
                CategoryData data = (CategoryData)tvLevel2.SelectedValue;
                if (!data.has_children)
                {
                    _leaf_category_id = data.id;
                }
            }
            if (this.tvLevel3.SelectedValue != null)
            {
                CategoryData data = (CategoryData)tvLevel3.SelectedValue;
                if (!data.has_children)
                {
                    _leaf_category_id = data.id;
                }
            }

            if (this.tvLevel4.SelectedValue != null)
            {
                CategoryData data = (CategoryData)tvLevel4.SelectedValue;
                if (!data.has_children)
                {
                    _leaf_category_id = data.id;
                }
            }

            if (_leaf_category_id == 0)
            {
                MessageBox.Show("请选择类目");
                return;
            }


            this.DialogResult = true;
            this.Close();
        }

        public long leaf_category_id
        {
            get
            {
                return _leaf_category_id;
            }
        }
    }
}
